/**
 * 用于解析英文句子评分结果<br>
 *
 * @module ChiVoxSDK/score
 * @author Don Li <kai.li@chivox.com>
 * @licence
 * Copyright (c) 2017 - 2020 ChiVox Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /**
 * 英文句子里的Word评分结果。
 */
class Word {
    constructor(data) { this.data = data; }
    getChar() { return this.data.char; }
    getDur() { return this.data.dur; }
    getFluency() { return this.data.fluency; }
    getStressRef() { return this.data.stressref; }
    getStressScore() { return this.data.stressscore; }
    getToneRef() { return this.data.toneref; }
    getToneScore() { return this.data.tonescore; }
    getLiaison() { return this.data.liaison; }
    getScore() { return this.data.score; }
}

 /**
 * 英文句子评分结果。
 */
class EnSentScore {
    constructor(data) { this.data = data; }

    getParams() { return this.data.params; }
    getRecordId() { return this.data.recordId; }
    getAudioUrl() { return this.data.audioUrl; }
    getRank() { return this.data.result.rank; }
    getOverall() { return this.data.result.overall; }
    getPron() { return this.data.result.pron; }
    getIntegrity() { return this.data.result.integrity; }
    getAccuracy() { return this.data.result.accuracy; }
    getFluency() { return this.data.result.fluency.overall; }
    getRhythm() { return this.data.result.rhythm.overall; }

    getWordSize() {
        if (typeof this.data.result["chars"] != "undefined" && this.data.result["chars"].length > 0) {
            return this.data.result["chars"].length;
        } else {
            if (typeof this.data.result["details"] != "undefined" && this.data.result["details"].length > 0) {
                return this.data.result["details"].length;
            } else {
                return 0;
            }
        }
    }

    getWord(i) {
        if (typeof this.data.result["chars"] != "undefined" && this.data.result["chars"].length > 0) {
            var charI = {
                "char": "",
                "dur": 0,
                "fluency": 0,
                "score": 0,
                "senseref": 0,
                "sensescore": 0,
                "stressref": 0,
                "stressscore": 0,
                "toneref": 0,
                "tonescore": 0,
                "liaison": 0
            };

            var passLength = 0;
            for (var j = 0; j < i; j++) {
                passLength += this.data.result["chars"][j].normalize.length;
            }

            var cl = this.data.result["chars"][i];
            charI["char"] = cl["char"];
            for (var num = 0; num < cl.normalize.length; num++) {
                var obj = this.data.result["details"][passLength + num];
                charI["dur"] += obj["dur"];
                charI["fluency"] += obj["fluency"];
                charI["score"] += obj["score"];
                charI["senseref"] += obj["senseref"];
                charI["sensescore"] += obj["sensescore"];
                charI["stressref"] += obj["stressref"];
                charI["stressscore"] += obj["stressscore"];
                charI["toneref"] += obj["toneref"];
                charI["tonescore"] += obj["tonescore"];
                charI["liaison"] += obj["liaison"];
            }

            charI["fluency"] = Math.round(charI["fluency"] / cl.normalize.length);
            charI["score"] = Math.round(charI["score"] / cl.normalize.length);
            charI["sensescore"] = Math.round(charI["sensescore"] / cl.normalize.length);
            charI["stressscore"] = Math.round(charI["stressscore"] / cl.normalize.length);
            charI["tonescore"] = Math.round(charI["tonescore"] / cl.normalize.length);

            return new Word(charI);
        } else {
            var charI = this.data.result["details"][i];
            return new Word(charI);
        }
    }

    getCharSize() { return this.data.chars.length; }
    getChar(i) { return new Char(this.data.chars[i]); }

    hasError() {
        return
            (
             typeof this.data.result == 'undefined' ||
             typeof this.data.result.err != 'undefined' ||
             typeof this.data.result.error != 'undefined' ||
             typeof this.data.result.errID != 'undefined'
            );
    }
    getErrorCode() {
        if (typeof this.data.result != 'undefined' && typeof this.data.result.errID != 'undefined')
            return this.data.result.errID;
        else if (typeof this.data.result.error != 'undefined')
            return this.data.result.error;
    }
}

export default EnSentScore
